package com.heima.wemedia.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.article.IArticleClient;
import com.heima.model.article.vos.StatisticsVo;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmStatistics2Dto;
import com.heima.model.wemedia.dtos.WmStatistics3Dto;
import com.heima.model.wemedia.dtos.WmStatisticsDto;
import com.heima.model.wemedia.pojos.WmNewsStatistics;
import com.heima.model.wemedia.vo.WmArticleStatisticsVo;
import com.heima.wemedia.mapper.WmArticleStatisticsMapper;
import com.heima.wemedia.service.WmArticleStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@Transactional
public class WmArticleStatisticsServiceImpl extends ServiceImpl<WmArticleStatisticsMapper, WmNewsStatistics> implements WmArticleStatisticsService {

    @Autowired
    private IArticleClient articleClient;

    @Autowired
    private HttpServletRequest httpServletRequest;
    @Autowired
    private WmArticleStatisticsMapper wmArticleStatisticsMapper;


    /**
     * 统计图文数据
     * @param dto
     * @return
     */
    @Override
    public ResponseResult newsDimension(WmStatisticsDto dto) {

        // 检查参数
        if (dto == null) {
            return ResponseResult.errorResult(404, "参数无效");
        }

        // 获取用户id
        String userId = httpServletRequest.getHeader("userId");

        // 封装查询条件
        WmStatistics3Dto wmStatistics3Dto = new WmStatistics3Dto();
        BeanUtils.copyProperties(dto, wmStatistics3Dto);
        wmStatistics3Dto.setUserId(Integer.valueOf(userId));


        // feign 远程调用 根据条件查询文章
        List<WmNewsStatistics> wmNewsStatisticsList = articleClient.findByPublishTime(wmStatistics3Dto);

        /*// 存入数据库
        if (wmNewsStatisticsList != null || wmNewsStatisticsList.size() > 0){
            for (WmNewsStatistics wmNewsStatistics : wmNewsStatisticsList) {
                wmArticleStatisticsMapper.insert(wmNewsStatistics);
            }
        }*/

        long count = wmNewsStatisticsList.stream().count();
        int collectSum = wmNewsStatisticsList.stream().mapToInt(WmNewsStatistics::getCollection).sum();
        int likesSum = wmNewsStatisticsList.stream().mapToInt(WmNewsStatistics::getLikes).sum();
        //int forwardSum = wmNewsStatisticsList.stream().mapToInt(WmNewsStatistics::getFollow).sum();

        StatisticsVo statisticsVo = new StatisticsVo();
        statisticsVo.setCollectNum(collectSum);
        statisticsVo.setLikesNum(likesSum);
        statisticsVo.setPublishNum(wmNewsStatisticsList.size());
        statisticsVo.setForwardNum(10);

        return ResponseResult.okResult(statisticsVo);
    }

    /**
     * 图文统计-文章详细统计
     * @param dto
     * @return
     */
    @Override
    public ResponseResult newsPage(WmStatistics2Dto dto) {

        // 获取用户id
        String userId = httpServletRequest.getHeader("userId");

        // 封装查询条件
        WmStatistics3Dto wmStatistics3Dto = new WmStatistics3Dto();
        BeanUtils.copyProperties(dto, wmStatistics3Dto);
        wmStatistics3Dto.setUserId(Integer.valueOf(userId));

        // feign 远程调用 根据条件查询文章
        List<WmNewsStatistics> wmNewsStatisticsList = articleClient.findByPublishTime(wmStatistics3Dto);

        // 创建一个封装集合
        List<WmArticleStatisticsVo> wmArticleStatisticsVoList = new ArrayList<>();
        if (wmNewsStatisticsList != null && wmNewsStatisticsList.size() > 0){

            // 数据拷贝
            for (WmNewsStatistics wmNewsStatistics : wmNewsStatisticsList) {
                WmArticleStatisticsVo vo = new WmArticleStatisticsVo();
                BeanUtils.copyProperties(wmNewsStatistics, vo);
                vo.setId(wmNewsStatistics.getArticle() + "");
                vo.setTitle(wmNewsStatistics.getArticleTitle());

                // 添加到集合
                wmArticleStatisticsVoList.add(vo);
            }
        }

        // 检查参数
        if (dto == null) {
            return ResponseResult.errorResult(404, "无效参数");
        }


        // 分页
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getPage(), wmArticleStatisticsVoList.size());
        responseResult.setData(wmArticleStatisticsVoList);

        return responseResult;
    }
}
